package com.daily.pump.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.daily.pump.R;
import com.daily.pump.constants.ExtraConstants;
import com.xunxi.mimi.utils.ToastUtil;


/**
 * Desc:我的-设置视频聊天价格
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class WifiSelectDialog extends DialogFragment {

    private ScanDialogListener mScanDialogListener;

    private EditText mNameEt;

    public void setScanDialogListener(ScanDialogListener listener) {
        this.mScanDialogListener = listener;
    }

    public static WifiSelectDialog newInstance(String scanResult) {
        WifiSelectDialog scanResultNextDialog = new WifiSelectDialog();
        Bundle bundle = new Bundle();
        bundle.putString(ExtraConstants.DEVICE_NAME, scanResult);
        scanResultNextDialog.setArguments(bundle);
        return scanResultNextDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismissAllowingStateLoss();
                if (mScanDialogListener != null){
                    mScanDialogListener.onDismiss();
                }
            }
        });
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_scan_result_next, container, false);
        initData(dialogView);
        return dialogView;
    }

    private void initData(View dialogView) {
        mNameEt = dialogView.findViewById(R.id.name_et);
        RecyclerView historyRv = dialogView.findViewById(R.id.history_rv);
        historyRv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        dialogView.findViewById(R.id.next_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNext();
            }
        });

        Bundle bundle = getArguments();
        if (bundle != null){
            String name = bundle.getString(ExtraConstants.DEVICE_NAME);
            if (!TextUtils.isEmpty(name)){
                mNameEt.setText(name);
                mNameEt.setSelection(name.length());
            }
        }
    }

    private void onNext() {
        String name = mNameEt.getText().toString().trim();
        if (!TextUtils.isEmpty(name)){
            if (mScanDialogListener != null){
                mScanDialogListener.onTurnLogin(name);
            }
            dismissAllowingStateLoss();
        }else {
            ToastUtil.show(getActivity(), "please input device name!");
        }
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, WifiSelectDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    public interface ScanDialogListener{
        void onTurnLogin(String deviceName);
        void onDismiss();
    }
}

